package com.bitcola.chainserver.chain.trx.core;



import java.math.BigInteger;
import java.util.List;
import com.bitcola.chainserver.chain.trx.api.*;

public class Trc20Demo{
  public static void main(String[] args) throws Exception{
    TronKit kit = new TronKit(
      TronApi.mainNet(),
      Credential.fromPrivateKey("8D9142B97B38F992B4ADF9FB3D0DD527B1F47BE113C6D0B5C32A0571EF1E7B5F")
    );

    String contractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
    String toAddress = "TNTTxEBeevfHJrnfui6eWY5fpgQDo5NRLt";

    System.out.printf("token contract address: %s\n",contractAddress);

    Trc20 token = kit.trc20(contractAddress);

    System.out.println("query trc20 token info...");
    String name = token.name();

   var de= token.decimals();
    System.out.printf("name: %s\n",name);
    String symbol = token.symbol();
    System.out.printf("symbol: %s\n",symbol);
    BigInteger decimals = token.decimals();
    System.out.printf("decimals: %s\n",decimals);
    BigInteger totalSupply = token.totalSupply();
    System.out.printf("total supply: %s\n",totalSupply);

    System.out.printf("from address: %s\n",kit.getDefaultAddress());
    System.out.printf("to address: %s\n",toAddress);

    System.out.println("balance before transfer...");
    BigInteger b1 = token.balanceOf(toAddress);
    System.out.printf("balance: %s\n",b1);

    System.out.println("transfer token...");
    BigInteger value = new BigInteger("8");
    TransactionResult ret = token.transfer(toAddress,value);
    System.out.printf("token transfer tx result: %s\n",ret);

    System.out.println("balance after transfer...");
    BigInteger b2 = token.balanceOf(toAddress);
    System.out.printf("balance: %s\n",b2);

    System.out.println("fetch token events");
    long since = System.currentTimeMillis() - 60000*2;
    List<ContractEvent> events = token.events(22502278l);
    System.out.printf("num of events: %d\n",events.size());
    for(ContractEvent e: events){
      System.out.printf("event name:%s | timestamp: %d\n", e.eventName,e.blockTimestamp);
      e.resultType.forEach((k,v) -> System.out.printf("\t%s: %s\n",k,e.result.get(k)));
    }
  }
}